/*
 * Copyright [2010] [GP Software]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * UrlLauncherPanel.java | Created on March 10, 2010
 */
package org.gp.ui;

import org.apache.commons.lang.StringUtils;
import org.gp.util.BrowserLauncher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Panel for searching the Wiki
 *
 * @author Paul Augustine C. Mendoza
 */
public class UrlLauncherPanel implements ActionListener {

    /** The page search type constant */
    private static final String SEARCH_TYPE_PAGE = "display/";

    /** The label search type constant */
    private static final String SEARCH_TYPE_LABEL = "label/";

    /** The Wiki domain constant */
    private String domain;

    /** The space constant */
    private String space;

    /** The search field */
    private JTextField searchField;

    /** The search label button */
    private JButton searchLabelButton;

    /** The main panel */
    private JPanel mainPanel;

    /** The search page button */
    private JButton searchPageButton;

    /** The locale dropdown */
    private JComboBox localeComboBox;

    /**
     * The default constructor
     *
     * @param domain the domain
     * @param space  the domain space
     */
    public UrlLauncherPanel(String domain, String space) {
        this.domain = domain + "/";
        this.space = space + "/";
        this.searchLabelButton.addActionListener(this);
        this.searchPageButton.addActionListener(this);
    }

    /**
     * Returns the main panel
     *
     * @return the main panel
     */
    public JPanel getMainPanel() {
        return mainPanel;
    }

    /**
     * Performs the corresponding event action
     *
     * @param event action event
     */
    public void actionPerformed(ActionEvent event) {
        String type = (event.getSource().equals(this.searchPageButton)) ?
                SEARCH_TYPE_PAGE :
                SEARCH_TYPE_LABEL;
        String locale =
                (this.localeComboBox.getSelectedIndex() == 0) ? "en" : "pt";

        if (StringUtils.isBlank(this.searchField.getText())) {
            return; // Do nothing...
        }

        String param =
                StringUtils.replace(this.searchField.getText(), " ", "+");
        // Add locale if page search
        if (type.equalsIgnoreCase(SEARCH_TYPE_PAGE)) {
            param = param + "-" + locale;
        } else {
            param = StringUtils.replace(this.searchField.getText(), " ", "_") +
                    "_" + locale;
        }

        System.out
                .println("Opening " + this.domain + type + this.space + param);
        try {
            BrowserLauncher.openURL(this.domain + type + this.space + param);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT
     * edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        final JPanel spacer1 = new JPanel();
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.VERTICAL;
        mainPanel.add(spacer1, gbc);
        searchField = new JTextField();
        searchField.setColumns(15);
        searchField.setText("");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(searchField, gbc);
        final JLabel label1 = new JLabel();
        label1.setText("Search:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(label1, gbc);
        final JPanel spacer2 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(spacer2, gbc);
        final JLabel label2 = new JLabel();
        label2.setText("Locale:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(label2, gbc);
        final JPanel spacer3 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(spacer3, gbc);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.gridheight = 2;
        gbc.fill = GridBagConstraints.BOTH;
        mainPanel.add(panel1, gbc);
        panel1.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEmptyBorder(), ""));
        searchPageButton = new JButton();
        searchPageButton.setText("Get Page");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(searchPageButton, gbc);
        final JPanel spacer4 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(spacer4, gbc);
        searchLabelButton = new JButton();
        searchLabelButton.setText("Search Label");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel1.add(searchLabelButton, gbc);
        localeComboBox = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 =
                new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("English");
        defaultComboBoxModel1.addElement("Portuguese");
        localeComboBox.setModel(defaultComboBoxModel1);
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(localeComboBox, gbc);
        final JPanel spacer5 = new JPanel();
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(spacer5, gbc);
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return mainPanel;
    }
}
